#include "prosetpage.h"
#include "ui_prosetpage.h"
#include <QLineEdit>
#include <QDir>
#include <QFileDialog>

ProSetPage::ProSetPage(QWidget *parent)
    : QWizardPage(parent)
    , ui(new Ui::ProSetPage)
{
    ui->setupUi(this);
    //注册域，"*"可以让lineEdit有输入的情况下才能下一步。
    registerField("proPath", ui->path_lineEdit);
    registerField("proName*", ui->name_lineEdit);
    //发出信号，暂时不知道后续逻辑。。。okokok
    connect(ui->name_lineEdit, &QLineEdit::textEdited,this,&ProSetPage::completeChanged);
    connect(ui->path_lineEdit, &QLineEdit::textEdited, this, &ProSetPage::completeChanged);
    QString curPath = QDir::currentPath();
    ui->path_lineEdit->setText(curPath);
    ui->path_lineEdit->setCursorPosition(ui->path_lineEdit->text().size());//设置光标位置
    ui->name_lineEdit->setClearButtonEnabled(true);
    ui->path_lineEdit->setClearButtonEnabled(true);
}

ProSetPage::~ProSetPage()
{
    delete ui;
}

bool ProSetPage::isComplete() const
{
    qDebug() <<"isComplete?";

    if(ui->name_lineEdit->text() == "" || ui->path_lineEdit->text() == ""){
        return false;
    }
    //判断是否文件夹是否合理
    QDir dir(ui->path_lineEdit->text());
    if(!dir.exists())
    {
        //qDebug()<<"file path is not exists" << endl;
        ui->tips->setText("project path is not exists");
        return false;
    }
    QString absFilePath = dir.absoluteFilePath(ui->name_lineEdit->text());
        qDebug() << "absFilePath is " << absFilePath ;//为什么触发两次？
    QDir dist_dir(absFilePath);
    if(dist_dir.exists()){
        //加上文件名之后的目录如果存在
        ui->tips->setText("project has exists, change path or name!");
        return false;
    }
    ui->tips->setText("");
    return QWizardPage::isComplete();
}

void ProSetPage::get_name_path(QString &name, QString &path)
{
    name = ui->name_lineEdit->text();
    path = ui->path_lineEdit->text();
}

void ProSetPage::on_pushButton_clicked()
{
    QFileDialog file_dialog;
    file_dialog.setFileMode(QFileDialog::Directory);
    file_dialog.setWindowTitle("选择保存位置");
    auto path = QDir::currentPath();
    file_dialog.setDirectory(path);

    QStringList filepaths;
    if(file_dialog.exec()){
        filepaths = file_dialog.selectedFiles();
    }

    if(filepaths.size()<=0){
        return;
    }

    QString save_path = filepaths.at(0);
    ui->path_lineEdit->setText(save_path);
}

